package com.multi.thread.chapter04.demo10;

import java.util.concurrent.locks.ReentrantLock;

/**
 *	isHeldByCurrentThread()：查询当前线程是否保持此锁定
 */
public class TestIsHeldByCurrentThread {
	
	private ReentrantLock lock;

	public TestIsHeldByCurrentThread(boolean isFair) {
		lock = new ReentrantLock(isFair);
	}
	
	public void serviceMethod() {
		try {
			System.out.println(lock.isHeldByCurrentThread());
			lock.lock();
			System.out.println(lock.isHeldByCurrentThread());
		} finally {
			lock.unlock();
		}
	}
	
	public static void main(String[] args) {
		final TestIsHeldByCurrentThread service = new TestIsHeldByCurrentThread(true);
		Runnable run = new Runnable() {
			@Override
			public void run() {
				service.serviceMethod();
			}
		};
		Thread thread = new Thread(run);
		thread.start();
	}

}
